﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Text;

namespace BarleyBreak
{
    public class Engine
    {
        public const int m_cellsCount = 9;
        public const int m_scale = 150;
        private Cell[] m_cells = new Cell[m_cellsCount];
        private MainPage m_page;

      
        public Cell GetCell(int i)
        {
            return m_cells[i];
        }
        
        public void Init(MainPage page)
        {
            m_page = page;
            InitCells();
            ShuffleCells();
        }

        private void InitCells()
        {
            for (int i = 0; i < m_cellsCount; ++i)
            {  
                StringBuilder picturePath = new StringBuilder();
                int pictureNumber = i + 1;
                picturePath.Append("pictures/");
                picturePath.Append(pictureNumber.ToString());
                picturePath.Append(".png");

                m_cells[i] = new Cell();
                m_cells[i].Init(i, picturePath.ToString());
                
                if (i + 1 == m_cellsCount)
                {
                    m_cells[i].SetEmptyCell();
                }
            }
        }

       

        private void ShuffleCells()
        {
            Random rand = new Random(System.DateTime.Now.Second);
            int[] positions = { 0, 1, 2, 3, 4, 5, 6, 7, 8 };


            for (int i = 0; i < 100; ++i)
            {
                int r = rand.Next(0, m_cellsCount - 1);
                int temp = positions[r];
                positions[r] = positions[r + 1];
                positions[r + 1] = temp;
            }
            
            int index = 0;
            foreach(Cell cell in m_cells)
            {
                cell.SetCoordinates(positions[index]);
                ++index;
            }
            
             
        }

        public bool IsCellEmpty(int x, int y)
        {
            if (x >= 0 && y >= 0)
            {
                Cell cell = GetCell(x, y);
                if (cell != null)
                {
                    return cell.IsCellEmpty();
                }                
            }

            return false;
        }


        public Cell GetCell(int x, int y)
        {
            foreach (Cell cell in m_cells)
            {
                int x1 = cell.X;
                int y1 = cell.Y;

                if (x == x1 && y == y1)
                {
                    return cell;
                }
            }

            return null;
        }

        public bool GameOver()
        {
            foreach (Cell cell in m_cells)
            {
                if (!cell.Correct())
                {
                    return false;
                }
            }

            return true;
        }

        public void Passage(int i)
        {
            Cell cell = GetCell(i);
            int x = cell.X;
            int y = cell.Y;

            int x1 = x + 1;
            int x2 = x - 1;

            int y1 = y + 1;
            int y2 = y - 1;


            if (CheckCell(x, y1))
            {
                m_page.MoveCell(i, x, y1);
            }
            if (CheckCell(x, y2))
            {
                m_page.MoveCell(i, x, y2);
            }
            if (CheckCell(x2, y))
            {
                m_page.MoveCell(i, x2, y);
            }
            if (CheckCell(x1, y))
            {
                m_page.MoveCell(i, x1, y);
            }
        }

        private bool CheckCell(int x, int y)
        {
            return IsCellEmpty(x, y);
        }

       

    }
}
